package com.securitytrace.client.addresslistener;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.securitytrace.client.ManagerClient;
import com.securitytrace.dataModel.AddressDataModel;
import com.securitytrace.dataModel.AddressModelToAdd;
import com.securitytrace.init.Init;
import com.securitytrace.listener.AddAddressButtonListener;
import com.securitytrace.listener.UpdateAddressButtonListener;
import com.securitytrace.model.Address;
import com.securitytrace.popupwindow.AddressWindow;

public class AddressActionListener {
	
	private ManagerClient managerClient;
	
	public void addAddress() {
		
		AddressWindow addressFrame = new AddressWindow(managerClient,true);
		AddAddressButtonListener listener = new AddAddressButtonListener(addressFrame);
		addressFrame.setSubmitButton(listener);
		
		AddressModelToAdd addressModelToAdd = new AddressModelToAdd();
		addressModelToAdd.setDataVector(null);
		addressFrame.setAddressModel(addressModelToAdd);
		addressFrame.getAddressTable().setModel(addressModelToAdd);
		addressFrame.setVisible(true);
	}
	
	public void deleteAddress() {

		AddressDataModel model = managerClient.getAddressModel();
		int row = managerClient.getAddressPanel().getAddressTable().getSelectedRow();
		if(row >= 0) {
			Address address = managerClient.getAddressModel().getRow(row);
			int currentPage = model.getAddressCurrentPage();
			int pageSize = managerClient.getPageSize();
			int totalPage = 1;
			
			try {
				Init.getAddressService().deleteById(address.getId());
				List<Address> addresses = new ArrayList<Address>();
				addresses = Init.getAddressService().getListByPage((currentPage - 1)* pageSize, pageSize);
				if((0 >= addresses.size() || null == addresses) && currentPage >1) {
					addresses = Init.getAddressService().getListByPage((currentPage - 2)* pageSize, pageSize);
					model.setAddressCurrentPage(currentPage - 1);
				}
				long addressNumber = Init.getAddressService().getTotalCount();
				if((addressNumber % pageSize) == 0) {
					totalPage = (int) (addressNumber / pageSize);
				}else {
					totalPage = (int) (addressNumber / pageSize) + 1;
				}
				model.setDataVector(addresses);
				model.setAddressTotal(totalPage);
				System.out.println("delete address info");
				managerClient.getAddressPanel().getAddressPageInfoLabel().setText(model.getAddressPageInfo());
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(null,"操作失败");
				return ;
			}
		}
	}

	public void updateAddress() {
		AddressDataModel dataModel = managerClient.getAddressModel();
		int row = managerClient.getAddressPanel().getAddressTable().getSelectedRow();
		if(row >= 0) {
			Address address = dataModel.getRow(row);
			AddressWindow addressFrame = new AddressWindow(managerClient, true);
			UpdateAddressButtonListener listener = new UpdateAddressButtonListener(addressFrame);
			listener.setAddress(address);
			addressFrame.getAddressNameTextField().setText(address.getAddressName());
			AddressModelToAdd model = new AddressModelToAdd();
			model.setDataVector(address.getOtherInfo());
			addressFrame.setAddressModel(model);
			addressFrame.getAddressTable().setModel(model);
			addressFrame.setSubmitButton(listener);
			addressFrame.setVisible(true);
			if (null != managerClient.getResultAddress()) {
				dataModel.setValueAtRow(managerClient.getResultAddress(), row);
			}
		}
	}


	public void homePageAction() {
		AddressDataModel model = managerClient.getAddressModel();
		if (model.getAddressCurrentPage() > 1) {
			model.setDataVector(Init.getAddressService().getListByPage(0,managerClient.getPageSize()));
			model.setAddressCurrentPage(1);
			managerClient.getAddressPanel().getAddressTable().setModel(model);
			managerClient.getAddressPanel().getAddressPageInfoLabel().setText(model.getAddressPageInfo());
		}
	}
	
	public void previousPageAction() {
		AddressDataModel model = managerClient.getAddressModel();
		int currentPage = model.getAddressCurrentPage();
		if (currentPage > 1) {
			model.setAddressCurrentPage(currentPage - 1);
			model.setDataVector(Init.getAddressService().getListByPage((currentPage - 2)* managerClient.getPageSize(),managerClient.getPageSize()));
			managerClient.getAddressPanel().getAddressTable().setModel(model);
			managerClient.getAddressPanel().getAddressPageInfoLabel().setText(model.getAddressPageInfo());
		}
	}
	
	public void nextPageAction() {
		AddressDataModel model = managerClient.getAddressModel();
		int currentPage = model.getAddressCurrentPage();
		int totalPage = model.getAddressTotal();
		if (currentPage < totalPage) {
			model.setAddressCurrentPage(currentPage + 1);
			model.setDataVector(Init.getAddressService().getListByPage(currentPage * managerClient.getPageSize(),managerClient.getPageSize()));
			managerClient.getAddressPanel().getAddressTable().setModel(model);
			managerClient.getAddressPanel().getAddressPageInfoLabel().setText(model.getAddressPageInfo());
		}
	}
	
	public void lastPageAction() {
		AddressDataModel model = managerClient.getAddressModel();
		int currentPage = model.getAddressCurrentPage();
		int totalPage = model.getAddressTotal();
		if (currentPage < totalPage) {
			model.setAddressCurrentPage(totalPage);
			model.setDataVector(Init.getAddressService().getListByPage((totalPage - 1) * managerClient.getPageSize(),managerClient.getPageSize()));
			model.setAddressCurrentPage(totalPage);
			managerClient.getAddressPanel().getAddressTable().setModel(model);
			managerClient.getAddressPanel().getAddressPageInfoLabel().setText(model.getAddressPageInfo());
		}
	}
	
	/***以下为get/set方法**/
	public ManagerClient getManagerClient() {
		return managerClient;
	}
	public void setManagerClient(ManagerClient managerClient) {
		this.managerClient = managerClient;
	}
}
